// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.objects;

import fmod.fmod.Audio;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.iso.IsoCell;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.sprite.IsoSprite;

public class IsoJukebox extends IsoObject {
    private Audio JukeboxTrack = null;
    private boolean IsPlaying = false;
    private float MusicRadius = 30.0F;
    private boolean Activated = false;
    private int WorldSoundPulseRate = 150;
    private int WorldSoundPulseDelay = 0;

    public IsoJukebox(IsoCell cell, IsoGridSquare sq, IsoSprite spr) {
        super(cell, sq, spr);
    }

    @Override
    public String getObjectName() {
        return "Jukebox";
    }

    public IsoJukebox(IsoCell cell) {
        super(cell);
    }

    public IsoJukebox(IsoCell cell, IsoGridSquare sq, String gid) {
        super(cell, sq, gid);
        this.JukeboxTrack = null;
        this.IsPlaying = false;
        this.Activated = false;
        this.WorldSoundPulseDelay = 0;
    }

    @Override
    public void addToWorld() {
        super.addToWorld();
        this.getCell().addToStaticUpdaterObjectList(this);
    }

    public void SetPlaying(boolean ShouldPlay) {
        if (this.IsPlaying != ShouldPlay) {
            this.IsPlaying = ShouldPlay;
            if (this.IsPlaying && this.JukeboxTrack == null) {
                String string = null;
                switch (Rand.Next(4)) {
                    case 0:
                        string = "paws1";
                        break;
                    case 1:
                        string = "paws2";
                        break;
                    case 2:
                        string = "paws3";
                        break;
                    case 3:
                        string = "paws4";
                }

                this.JukeboxTrack = zombie.SoundManager.instance.PlaySound(string, false, 0.0F);
            }
        }
    }

    @Override
    public boolean onMouseLeftClick(int x, int y) {
        IsoPlayer isoPlayer = IsoPlayer.getInstance();
        if (isoPlayer == null || isoPlayer.isDead()) {
            return false;
        } else if (IsoPlayer.getInstance().getCurrentSquare() == null) {
            return false;
        } else {
            float _float = 0.0F;
            int _int = Math.abs(this.square.getX() - IsoPlayer.getInstance().getCurrentSquare().getX())
                + Math.abs(
                    this.square.getY()
                        - IsoPlayer.getInstance().getCurrentSquare().getY()
                        + Math.abs(this.square.getZ() - IsoPlayer.getInstance().getCurrentSquare().getZ())
                );
            if (_int < 4) {
                if (!this.Activated) {
                    if (Core.NumJukeBoxesActive < Core.MaxJukeBoxesActive) {
                        this.WorldSoundPulseDelay = 0;
                        this.Activated = true;
                        this.SetPlaying(true);
                        Core.NumJukeBoxesActive++;
                    }
                } else {
                    this.WorldSoundPulseDelay = 0;
                    this.SetPlaying(false);
                    this.Activated = false;
                    if (this.JukeboxTrack != null) {
                        zombie.SoundManager.instance.StopSound(this.JukeboxTrack);
                        this.JukeboxTrack.stop();
                        this.JukeboxTrack = null;
                    }

                    Core.NumJukeBoxesActive--;
                }
            }

            return true;
        }
    }

    @Override
    public void update() {
        if (IsoPlayer.getInstance() != null) {
            if (IsoPlayer.getInstance().getCurrentSquare() != null) {
                if (this.Activated) {
                    float float0 = 0.0F;
                    int _int = Math.abs(this.square.getX() - IsoPlayer.getInstance().getCurrentSquare().getX())
                        + Math.abs(
                            this.square.getY()
                                - IsoPlayer.getInstance().getCurrentSquare().getY()
                                + Math.abs(this.square.getZ() - IsoPlayer.getInstance().getCurrentSquare().getZ())
                        );
                    if ((float)_int < this.MusicRadius) {
                        this.SetPlaying(true);
                        float0 = (this.MusicRadius - (float)_int) / this.MusicRadius;
                    }

                    if (this.JukeboxTrack != null) {
                        float float1 = float0 + 0.2F;
                        if (float1 > 1.0F) {
                            float1 = 1.0F;
                        }

                        zombie.SoundManager.instance.BlendVolume(this.JukeboxTrack, float0);
                        if (this.WorldSoundPulseDelay > 0) {
                            this.WorldSoundPulseDelay--;
                        }

                        if (this.WorldSoundPulseDelay == 0) {
                            zombie.WorldSoundManager.instance
                                .addSound(IsoPlayer.getInstance(), this.square.getX(), this.square.getY(), this.square.getZ(), 70, 70, true);
                            this.WorldSoundPulseDelay = this.WorldSoundPulseRate;
                        }

                        if (!this.JukeboxTrack.isPlaying()) {
                            this.WorldSoundPulseDelay = 0;
                            this.SetPlaying(false);
                            this.Activated = false;
                            if (this.JukeboxTrack != null) {
                                zombie.SoundManager.instance.StopSound(this.JukeboxTrack);
                                this.JukeboxTrack.stop();
                                this.JukeboxTrack = null;
                            }

                            Core.NumJukeBoxesActive--;
                        }
                    }
                }
            }
        }
    }
}
